package com.zues.samples.dao.Impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import com.zues.samples.dao.BaseHibernateDAO;
import com.zues.samples.dao.ScoreDAO;
import com.zues.samples.model.PagerModel;
import com.zues.samples.model.Score;
import com.zues.samples.service.modelVO.ScoreVO;
import com.zues.samples.util.SystemContext;

@Repository
public class ScoreDAOImpl extends BaseHibernateDAO<Score> implements ScoreDAO{

	@SuppressWarnings("unchecked")
	@Override
	public List<Score> findByAllScore() throws Exception {
		String hql ="FROM Score";
		List<Score> scores = this.createQuery(hql).list();
		return scores;
	}

	@SuppressWarnings("unchecked")
	@Override
	public PagerModel<ScoreVO> findAllByPage() {
		PagerModel<ScoreVO> page = new PagerModel<ScoreVO>();
		ArrayList<ScoreVO> dates = new ArrayList<ScoreVO>();

		int offset = SystemContext.getOffset();
		int pageSize = SystemContext.getSize();
		
		List<Score> tempDates = this.createQuery("FROM Score").setFirstResult(offset).setMaxResults(pageSize).list();
		for (Score score : tempDates) {
			ScoreVO scoreVO = new ScoreVO();
			BeanUtils.copyProperties(score, scoreVO);
			scoreVO.setUserName(score.getUser().getUserName());
			scoreVO.setSubjectName(score.getSubject().getName());
			scoreVO.setSubjectDescription(score.getSubject().getDescription());
			scoreVO.setSubjectTestCount(score.getSubject().getTestCount());
			scoreVO.setSubjectTotalScore(score.getSubject().getTotalScore());
			scoreVO.setSubjectTotalTime(score.getSubject().getTotalTime());
			scoreVO.setSubjectStatus(score.getSubject().getStatus());
			dates.add(scoreVO);
		}
		page.setDates(dates);
		page.setOffset(offset);
		page.setPageSize(pageSize);

		// 获取总记录数
				long totalSize = (long) this.createQuery("select count(*) from Score").uniqueResult();
				page.setTotalSize(totalSize);

		return page;
	}

	@Override
	public Score showScore(Integer scoreId) throws Exception {
		Score score = new Score();
		score = this.get(scoreId);
		return score;
	}

}
